package offer

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"encoding/json"
)

type OffTransactionOfferItemStatPtrToOffTransactionOfferItemPtrFunc func(*sqlc.OffTransactionOfferItemStat) *OffTransactionOfferItem
type OffTransactionPtrToOffTransactionOrderPtrFunc func(*sqlc.OffTransaction) *OffTransactionOrder

// ConvOffTransactionOfferItemStatPtrToOffTransactionOfferItemPtr get func of *sqlc.OffTransactionOfferItemStat => *OffTransactionOfferItem
func (s *OfferComServiceImp) ConvOffTransactionOfferItemStatPtrToOffTransactionOfferItemPtr() OffTransactionOfferItemStatPtrToOffTransactionOfferItemPtrFunc {
	return func(ptr *sqlc.OffTransactionOfferItemStat) *OffTransactionOfferItem {
		if ptr == nil {
			return nil
		}
		var offerItemInfoPtr *OffItemInfo
		offerItemInfoPtr, _ = s.UnMarshalOfferItemStatlInfoToOffItemInfoPtr(ptr.OfferItemStatInfo)
		return &OffTransactionOfferItem{
			Off:              ptr.Off,
			OfferType:        ptr.OfferItemType,
			OfferItemType:    ptr.OfferItemType,
			OfferItemID:      ptr.OfferItemID,
			OfferItemInfoPtr: offerItemInfoPtr,
		}
	}
}

// MapOffTransactionOfferItemStatPtrsToOffTransactionOfferItemPtrs []*sqlc.OffTransactionOfferItemStat => []*OffTransactionOfferItem
func (s *OfferComServiceImp) MapOffTransactionOfferItemStatPtrsToOffTransactionOfferItemPtrs(list []*sqlc.OffTransactionOfferItemStat, conv OffTransactionOfferItemStatPtrToOffTransactionOfferItemPtrFunc) []*OffTransactionOfferItem {
	ptrs := make([]*OffTransactionOfferItem, len(list))
	for i, entity := range list {
		ptrs[i] = conv(entity)
	}
	return ptrs
}

// ConvOffTransactionPtrToOffTransactionOrderPtr get func of *sqlc.OffTransaction => *OffTransactionOrder
func (s *OfferComServiceImp) ConvOffTransactionPtrToOffTransactionOrderPtr(
	offTransactionOfferItemStatPtrs []*sqlc.OffTransactionOfferItemStat,
) OffTransactionPtrToOffTransactionOrderPtrFunc {
	return func(ptr *sqlc.OffTransaction) *OffTransactionOrder {
		if ptr == nil {
			return nil
		}
		return &OffTransactionOrder{
			Off:           ptr.Off,
			OwnerID:       ptr.OwnerID,
			OutOrderNo:    ptr.OutOrderNo,
			OfferItemPtrs: s.MapOffTransactionOfferItemStatPtrsToOffTransactionOfferItemPtrs(offTransactionOfferItemStatPtrs, s.ConvOffTransactionOfferItemStatPtrToOffTransactionOfferItemPtr()),
		}
	}
}

// MapOOffTransactionPtrsToOffTransactionOrderPtrs []*sqlc.OffTransaction => []*OffTransactionOrder
func (s *OfferComServiceImp) MapOOffTransactionPtrsToOffTransactionOrderPtrs(offTransactionPtrs []*sqlc.OffTransaction, offTransactionOfferItemStatPtrs []*sqlc.OffTransactionOfferItemStat) []*OffTransactionOrder {
	ptrs := make([]*OffTransactionOrder, len(offTransactionPtrs))
	for i, ptr := range offTransactionPtrs {
		offTransactionOfferItemStatPtrsOfTheOne := s.entityHub.OffTransactionService.FilterOffTransactionOfferItemStatPtrs(offTransactionOfferItemStatPtrs, func(otoi *sqlc.OffTransactionOfferItemStat) bool {
			return otoi.TransactionID == ptr.ID
		})
		ptrs[i] = s.ConvOffTransactionPtrToOffTransactionOrderPtr(offTransactionOfferItemStatPtrsOfTheOne)(ptr)
	}
	return ptrs
}

func (s *OfferComServiceImp) UnMarshalOfferItemStatlInfoToOffItemInfoPtr(offerItemTrialInfoBytes []byte) (ptr *OffItemInfo, err error) {
	ptr = &OffItemInfo{}
	err = json.Unmarshal(offerItemTrialInfoBytes, ptr)
	if err != nil {
		return
	}
	return
}
